Coin Toss Simulator code
#include
using namespace std;
int main() {
const int CORRECT_PIN = 1234;
int enteredPin, attempts = 0;
double balance = 1000.0; // initial balance
int choice;
double amount;
cout << "Welcome to the ATM Simulator\n";
// PIN verification (max 3 attempts)
while (attempts < 3) {
cout << "Enter your 4-digit PIN: ";
cin >> enteredPin;
if (enteredPin == CORRECT_PIN) {
cout << "PIN accepted.\n\n";
break;
} else {
cout << "Incorrect PIN. Try again.\n";
attempts++;
}
}
if (attempts == 3) {
cout << "Too many incorrect attempts. Card blocked.\n";
return 0;
}
// ATM operations loop
do {
cout << "\n--- ATM Menu ---\n";
cout << "1. Check Balance\n";
cout << "2. Withdraw\n";
cout << "3. Deposit\n";
cout << "4. Exit\n";
cout << "Choose an option: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Your current balance is: $" << balance << "\n";
break;
case 2:
cout << "Enter amount to withdraw: $";
cin >> amount;
if (amount <= 0) {
cout << "Invalid amount.\n";
} else if (amount > balance) {
cout << "Insufficient funds.\n";
} else {
balance -= amount;
cout << "Withdrawal successful. New balance: $" << balance << "\n";
}
break;
case 3:
cout << "Enter amount to deposit: $";
cin >> amount;
if (amount <= 0) {
cout << "Invalid amount.\n";
} else {
balance += amount;
cout << "Deposit successful. New balance: $" << balance << "\n";
}
break;
case 4:
cout << "Thank you for using the ATM. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 4);
return 0;
}
Code output
Welcome to the ATM Simulator
Enter your 4-digit PIN: 1234
PIN accepted.
--- ATM Menu ---
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Choose an option: 1
Your current balance is: $1000
--- ATM Menu ---
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Choose an option: 2
Enter amount to withdraw: $500
Withdrawal successful. New balance: $500
--- ATM Menu ---
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Choose an option: 4
Thank you for using the ATM. Goodbye!